/**
 * 添加或修改补贴类型页面
 * @param {*} item 如果为null:添加新补贴类型,不为空时:修改补贴类型
 */
module.exports = function ($scope, $resource, $modalInstance, item, toaster) {

    $scope.obj = angular.copy(item) || {};

    //确认保存
    $scope.ok = function() {
        if(!checkParam($scope.obj)) return;
        $resource('/api/ac/atc/policyService/saveSubsidyType', {}, {}).save($scope.obj, function(res){
            if (res.errcode === 0) {
                toaster.success({title: '', body: '保存成功'});
                $modalInstance.close();
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
		});
    }
    
    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }

    //添加或修改补贴类型是检验数据合法性
	function checkParam(item) {
		if(!item.name){
			toaster.warning({'title': '', 'body': '补贴类型不能为空'});
			return false;
		}
		if(!item.ticket_num || item.ticket_num < 0){
			toaster.warning({'title': '', 'body': '票数量不合法'});
			return false;
		}
		if(item.room_num || item.room_num >= 0){ } else {
            toaster.warning({'title': '', 'body': '住房数量不合法'});
			return false;
        }
		return true;
	}

};